package com.example.administrator.runbook.fragments.community.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.runbook.R;
import com.example.administrator.runbook.fragments.community.model.Trends;

import java.util.List;

/**
 * Created by Administrator on 2016/11/8.
 */

public class TrendsListAdapter extends BaseAdapter {

    private List<Trends> list;
    private Context context;
    private LayoutInflater inflater;

    public TrendsListAdapter(Context context, List<Trends> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            if (getItemViewType(position) == 0) {
                convertView = inflater.inflate(R.layout.item_trends_main, parent, false);
                convertView.setTag(new ViewHold(convertView));
            } else if (getItemViewType(position) == 1) {
                convertView = inflater.inflate(R.layout.item_trends_text_main, parent, false);
                convertView.setTag(new ViewHoldText(convertView));
            }
        }
        if (getItemViewType(position) == 0) {
            ViewHold hold = (ViewHold) convertView.getTag();
            Trends trends = list.get(position);
            hold.bindView(position, trends);
        } else if (getItemViewType(position) == 1) {
            ViewHoldText hold = (ViewHoldText) convertView.getTag();
            Trends trends = list.get(position);
            hold.bindView(position, trends);
        }
        return convertView;
    }

    private static class ViewHold {

        private ImageView imageView;
        private TextView textView;

        public ViewHold(View itemView) {

            imageView = (ImageView) itemView.findViewById(R.id.item_trends_image);
            textView = (TextView) itemView.findViewById(R.id.item_trends_text);
        }

        public void bindView(int position, Trends data) {
            imageView.setImageResource(data.getImageId());
            textView.setText(data.getTitle());
        }
    }

    private static class ViewHoldText {

        private TextView textView;

        public ViewHoldText(View itemView) {

            textView = (TextView) itemView.findViewById(R.id.item_trends_text);
        }

        public void bindView(int position, Trends data) {
            textView.setText(data.getTitle());
        }
    }
}
